package com.example.mqcummter.controller;

import com.example.mqcummter.entity.BikebaseinfoEntity;
import com.example.mqcummter.mapper.BikeMapper;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Map;

@Component
@RabbitListener(queues = "TestDirectQueue")//监听的队列名称 TestDirectQueue
public class DirectReceiver {
    @Resource
    private BikeMapper bikeMapper;
    @Resource
    private RedisTemplate redisTemplate;
    @RabbitHandler
    public void process(String testMessage) {
        BikebaseinfoEntity bikebaseinfoEntity = bikeMapper.getById(testMessage);
        int insert = bikeMapper.insert(bikebaseinfoEntity);
        Integer del = bikeMapper.deleteRealtion(testMessage);

        Object delbike = redisTemplate.opsForValue().get("delbike");
        if (delbike!=null){
            int i = Integer.parseInt(delbike.toString());
            i=i+del;
            redisTemplate.opsForValue().set("delbike",i);
        }else {
            redisTemplate.opsForValue().set("delbike",del);
        }
        Object delbikeafter = redisTemplate.opsForValue().get("delbike");
        System.out.println("DirectReceiver消费者收到消息  : " + bikebaseinfoEntity.getAreaname()+bikebaseinfoEntity.getId()+"消除了积压更新"+del+"总计"+delbikeafter);
    }

}